package com.hidtechs.popfax.view;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.iconifiedList.IconifiedTextListAdapter;
import com.hidtechs.popfax.xml.ParsedExampleDataSet;
import com.hidtechs.popfax.xml.ParsedInboxDataSet;
import com.hidtechs.popfax.xml.ParsedOutboxDataSet;

public class Outbox extends ListActivity implements Runnable{


	Character class_type='o';
	protected Handler handler;
	protected HttpURLConnection conn=null;
	private ProgressDialog progressDialog;
	public HashMap<String,String> parameterValues;
	protected ArrayList arrayOfObjectOfParsedData;
	protected ArrayList arrayListretrieve[];
	String userName_shared_prefernece;
	String passWord_shared_prefernece;
	IconifiedTextListAdapter itlaOutbox;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		outboxData();
		
	}

	private void outboxData()
	{

		progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.OUTBOX_DATA_RETRIEVING_MSG, true,false);
		itlaOutbox = new IconifiedTextListAdapter(this);

		Thread thread = new Thread(this);
		thread.start(); 
		
		loginHandlerMethod();


	}

	public void run() {
		System.out.println("In Run");

		setMapValues();
		Requestor requestorForXMLParsing= new Requestor();
		arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(class_type,parameterValues);

		handler.sendEmptyMessage(0);		
	}


	public void loginHandlerMethod()
	{
		handler = new Handler() {

			public void handleMessage(Message msg) {
				progressDialog.dismiss();

				System.out.println("Handler");
				System.out.println(arrayOfObjectOfParsedData.size());
				arrayListretrieve = new ArrayList[arrayOfObjectOfParsedData.size()];
		        ArrayList<String> headingArrayList = new ArrayList<String>();
		        headingArrayList.add("TO");
		        headingArrayList.add("PAGES");
		        headingArrayList.add("DATE");
		        itlaOutbox.addItem(headingArrayList);
		        System.out.println("size="+arrayOfObjectOfParsedData.size());
		        for(int i=0;i<arrayOfObjectOfParsedData.size();i++)
		        {
		        	arrayListretrieve[i]= new ArrayList();
		        	arrayListretrieve[i].add(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getRecipient());
		        	System.out.println(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getRecipient());
		        	arrayListretrieve[i].add(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getPages());
		        	System.out.println(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getPages());
		        	arrayListretrieve[i].add(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getDate());
		        	System.out.println(((ParsedOutboxDataSet)(arrayOfObjectOfParsedData.get(i))).getDate());
		        	itlaOutbox.addItem(arrayListretrieve[i]);
		        }
		        
		        setListAdapter(itlaOutbox);
		 
			}

		};

	}

	void setMapValues() {

		loadPreference(); 
		parameterValues = new HashMap();
		parameterValues.put(Constants.URL_ACTION, Constants.OUTBOX_ACTION);
		parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
		parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
		parameterValues.put(Constants.DATE_FROM, "2010-01-05");
		parameterValues.put(Constants.DATE_TO, "2010-15-05");

	}



	protected void loadPreference()
	{
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getSharedPreferences("preferences", mode);
		userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
		System.out.println(userName_shared_prefernece);
		passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
		System.out.println(passWord_shared_prefernece);
	}

}
